package com.lb.shopguide.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.lb.shopguide.R;


/**
 * 章节前面的点和线
 * Created by Administrator on 2016/12/27.
 */

public class LbAddressPoint extends View {

    public static final int TYPE_START_LATEST = 1;
    public static final int TYPE_MIDDLE = 2;
    public static final int TYPE_END = 3;

    private int circleColor;
    private int lineColor;
    private Paint paint;

    private int type;

    public LbAddressPoint(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        lineColor = getResources().getColor(R.color.sg_text_color_hint);
        paint = new Paint();
        paint.setAntiAlias(true);
    }

    private void initType() {
        switch (type) {
            case TYPE_START_LATEST:
                circleColor = Color.rgb(255, 90, 90);
                break;
            case TYPE_MIDDLE:
            case TYPE_END:
                circleColor = getResources().getColor(R.color.sg_text_color_hint);
                break;
        }
    }

    public void setType(int type) {
        this.type = type;
        initType();
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int w = getWidth();
        int h = getHeight();

        int pl = getPaddingLeft();
        int pr = getPaddingRight();
        int pt = getPaddingTop();
        int pb = getPaddingBottom();

        int usableWidth = w - (pl + pr);
        int usableHeight = h - (pt + pb);

        int cx = pl + (usableWidth / 2);
        int cy = pt + (usableHeight / 2);

        int radius = 14;//半径
        switch (type) {
            case TYPE_START_LATEST://第一个
                paint.setColor(lineColor);
                canvas.drawLine(cx, cy, cx, h, paint);
                paint.setColor(circleColor);
                canvas.drawCircle(cx, cy, radius, paint);
                break;
            case TYPE_MIDDLE://中间
                paint.setColor(lineColor);
                canvas.drawLine(cx, 0, cx, h, paint);
                paint.setColor(circleColor);
                canvas.drawCircle(cx, cy, radius, paint);
                break;
            case TYPE_END://最后
                paint.setColor(lineColor);
                canvas.drawLine(cx, 0, cx, cy, paint);
                paint.setColor(circleColor);
                canvas.drawCircle(cx, cy, radius, paint);
                break;
        }

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int desiredWidth = 32;
        int desiredHeight = 80;

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int width;
        int height;

        //Measure Width
        if (widthMode == MeasureSpec.EXACTLY) {
            //Must be this size
            width = widthSize;
        } else if (widthMode == MeasureSpec.AT_MOST) {
            //Can't be bigger than...
            width = Math.min(desiredWidth, widthSize);
        } else {
            //Be whatever you want
            width = desiredWidth;
        }
        //Measure Height
        if (heightMode == MeasureSpec.EXACTLY) {
            //Must be this size
            height = heightSize;
        } else if (heightMode == MeasureSpec.AT_MOST) {
            //Can't be bigger than...
            height = Math.min(desiredHeight, heightSize);
        } else {
            //Be whatever you want
            height = desiredHeight;
        }
        //MUST CALL THIS
        setMeasuredDimension(width, height);
    }
}
